import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/models/response/content_response.dart';
import 'package:sainbainu/common/routes/routes.dart';
import 'package:sainbainu/common/widgets/widget.dart';

import 'index.dart';

class MyWorkPage extends StatelessWidget {
  const MyWorkPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    /// 这个必须在这里初始化 否则无法保持状态
    var controller = Get.put(MyWorkController());

    return EasyRefresh.builder(
      controller: controller.state.refreshController,
      onLoad: controller.onRichBottom,
      childBuilder: (context, physics) {
        return CustomScrollView(
          physics: physics,
          slivers: [
            _buildListView(),
          ],
        );
      },
    );
  }

  /// 列表
  Widget _buildListView() {
    return GetBuilder<MyWorkController>(
      id: 'contentList',
      builder: (_) {
        return SliverList.separated(
          itemCount: _.state.contentList.rows.length,
          separatorBuilder: (context, index) {
            return const SizedBox(height: 10);
          },
          itemBuilder: (context, index) {
            ContentResponse item = _.state.contentList.rows[index];

            return GestureDetector(
              child: _buildListItem(_, item),
              onTap: () {
                Get.toNamed(
                  AppRoutes.contentDetail,
                  arguments: {'noteId': item.noteId},
                );
              },
            );
          },
        );
      },
    );
  }

  /// 每一项
  Widget _buildListItem(MyWorkController controller, ContentResponse item) {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.only(
        top: 20,
        left: 20,
        right: 20,
        bottom: 5,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (item.text != null && item.text!.isNotEmpty)
            ContentCardText(item.text!),
          ImageVideoGrid(
            padding: const EdgeInsets.only(top: 12),
            imageLinkList: item.image ?? [],
            videoLink: item.videoCover,
          ),
          if (item.audio != null && item.audio!.isNotEmpty)
            Padding(
              padding: const EdgeInsets.only(top: 12),
              child: AudioWaveWidget(
                duration: item.audioDuration ?? 0,
              ),
            ),
          IconNumRow(
            giveFlag: item.giveFlag ?? 0,
            giveNum: item.giveNum ?? 0,
            collectionFlag: item.collectionFlag ?? 0,
            collectionNum: item.collectionNum ?? 0,
            visitNum: item.visitNum ?? 0,
            commentNum: item.commentNum ?? 0,
            shareNum: item.shareNum ?? 0,
            rightChild: GestureDetector(
              onTap: () => controller.onContentDel(item.noteId!),
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: Image.asset(
                  'assets/images/icon-num-del.png',
                  width: 20,
                  height: 20,
                ),
              ),
            ),
            onLikeTap: (isGive) => controller.onGiveHandle(isGive, item.noteId),
            onCollectionTap: (isGive) =>
                controller.onCollectionHandle(isGive, item.noteId),
          ),
        ],
      ),
    );
  }
}
